//
//  ViewController.m
//  STKeyValueStore
//
//  Created by mac on 15/1/17.
//  Copyright (c) 2015年 mac. All rights reserved.
//

#import "ViewController.h"
#import "STKeyValueStore.h"

#define DB_NAME @"test.db"
#define TABLE_NAME  @"user_table"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)createDataBase:(id)sender {
    STKeyValueStore *store = [[STKeyValueStore alloc] initDBWithName:DB_NAME];
}

- (IBAction)createTable:(id)sender {
    STKeyValueStore *store = [[STKeyValueStore alloc] initDBWithName:DB_NAME];
    [store createTableWithName:TABLE_NAME];
}

- (IBAction)insertRecord:(id)sender {
    NSString *key = @"1";
    NSDictionary *user = @{@"id":@1,@"name":@"lixiaoyi",@"age":@23};
    STKeyValueStore *store = [[STKeyValueStore alloc] initDBWithName:DB_NAME];
    [store putObject:user withId:key intoTable:TABLE_NAME];
}

- (IBAction)selectRecord:(id)sender {
    STKeyValueStore *store = [[STKeyValueStore alloc] initDBWithName:DB_NAME];
    NSString *key = @"1";
    NSDictionary *queryUser = [store getObjectById:key fromTable:TABLE_NAME];
    NSLog(@"query data result: %@", queryUser);
    _idLabel.text = [NSString stringWithFormat:@"%@",[queryUser objectForKey:@"id"]];
    _ageLabel.text = [NSString stringWithFormat:@"%@",[queryUser objectForKey:@"age"]];
    _nameLabel.text = [NSString stringWithFormat:@"%@",[queryUser objectForKey:@"name"]];
}

- (IBAction)deleteOne:(id)sender {
    STKeyValueStore *store = [[STKeyValueStore alloc] initDBWithName:DB_NAME];
    [store deleteObjectById:@"1" fromTable:TABLE_NAME];
}

- (IBAction)deleteAll:(id)sender {
    STKeyValueStore *store = [[STKeyValueStore alloc] initDBWithName:DB_NAME];
    [store deleteObjectsByIdArray:[NSArray arrayWithObjects:@"1", nil] fromTable:TABLE_NAME];
}
@end
